package com.bawei.club.test.bitmap

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bawei.club.R
import com.bawei.lib_image.GlideApp

/**
 1。bitmap尺寸压缩/二次采样：改变像素点的个数，减少内存大小
     第一次采：只采宽高----》计算压缩比例
     第二次采：按照压缩比例采像素点
 2.bitmap质量压缩：改变位深，改变文件大小
 3.大图长图区域加载：BitmapRegionDecoder结合手势图片区域加载
 */
class BitmapActivity : AppCompatActivity() {
    private lateinit var iv: ImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_bitmap)
        iv = findViewById(R.id.iv)
        loadBitmap()
        loadBitmap2()
        loadBitmap3()
//        GlideApp.with(this)
//            .load(R.drawable.a)
//            .override(20,20)
//            .into(null)

    }
    //ARGB_8888
    fun loadBitmap(){
        val options = BitmapFactory.Options()
        options.inPreferredConfig = Bitmap.Config.ARGB_8888
        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.b, options)
        Log.d("TAG", "loadBitmap:ARGB_8888 ${bitmap.byteCount} ")

    }

    //RGB_565
    fun loadBitmap2(){
        val options = BitmapFactory.Options()
        options.inPreferredConfig = Bitmap.Config.RGB_565
        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.b, options)
        Log.d("TAG", "loadBitmap:RGB_565 ${bitmap.byteCount} ")
    }

    //RGB_565 压缩
    fun loadBitmap3(){
        // 1000 * 1000   20*20
        //第一次只采集宽度和高度，不采像素点
        val options = BitmapFactory.Options()
        options.inPreferredConfig = Bitmap.Config.ARGB_8888
        options.inJustDecodeBounds = true //只采集宽度和高度
        BitmapFactory.decodeResource(resources, R.drawable.b, options)
        val outWidth = options.outWidth
        val outHeight = options.outHeight
        Log.d("TAG", "第一次采集宽高: $outWidth : $outHeight")
        //计算压缩比例 :2的幂次方
        var size = 1
        while (outWidth / size > 20 || outHeight / size > 20){
            size *= 2
        }
        Log.d("TAG", "size: $size")
        //第二次按照压缩比例采集像素点，像素点个数减少
        options.inJustDecodeBounds = false //采集像素点
        options.inSampleSize = size //设置压缩比例
        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.b, options)
        Log.d("TAG", "loadBitmap:二次采用后RGB_565 ${bitmap.byteCount} ")
        iv.setImageBitmap(bitmap)
    }





}